package cn.fsd.service.impl;

import cn.fsd.domain.Customer_address;
import cn.fsd.exception.CustomerException;
import cn.fsd.exception.CustomerExceptionCodeMsg;
import cn.fsd.mapper.Customer_addressMapper;
import cn.fsd.service.Customer_addressService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Customer_addressServiceImpl implements Customer_addressService {
    final Customer_addressMapper addressMapper;

    public Customer_addressServiceImpl(Customer_addressMapper addressMapper) {
        this.addressMapper = addressMapper;
    }

    @Override
    public List<Customer_address> getAddressOfCustomer(String id) {
        return addressMapper.selectByCustomerId(id);
    }

    @Override
    public void updateAddress(Customer_address address) throws CustomerException{
        if(address.getAddress_choice()==0){
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_0_CANNOT_UPDATE);
        }
        if(addressMapper.updateByPrimaryKey(address)==0){
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_UPDATE_FAIL);
        }
    }

    @Override
    public Customer_address getAddress(String id, Integer choice) {
        return addressMapper.selectByPrimaryKey(id, choice);
    }

    @Override
    public void addAddress(Customer_address address) throws CustomerException{
        if(address.getAddress_choice()==0){
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_0_CANNOT_ADD);
        }
        if(addressMapper.insert(address)==0){
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_INSERT_FAIL);
        }
    }

    @Override
    public void deleteAddress(String id, Integer choice) throws CustomerException{
        if(choice==0){
            throw new CustomerException(CustomerExceptionCodeMsg.ADDRESS_0_CANNOT_DELETE);
        }
        if(addressMapper.deleteByPrimaryKey(id, choice)==0){
            throw new CustomerException(CustomerExceptionCodeMsg.CUSTOMER_DELETE_FAIL);
        }
    }
    // TODO: 2023/6/28 计划采用validation校验字段
//    /**
//     * 检查Customer_address对象字段的完整性
//     * @param address 前端传来的对象
//     * @return 存在空字段返回True
//     */
//    private boolean checkIntegrity(Customer_address address) {
//        return address.getReceiver_address() == null || "".equals(address.getReceiver_address()) ||
//                address.getReceiver() == null || "".equals(address.getReceiver()) ||
//                address.getReceiver_phone() == null || "".equals(address.getReceiver_phone()) ||
//                address.getReceiver_postal_code() == null || "".equals(address.getReceiver_postal_code());
//    }
}
